package admin

import (
	"os"
	"strconv"
	"strings"
	"tcsc/models"
	"time"

	"github.com/astaxie/beego"
)

type CkeditorImageupload struct {
	beego.Controller
}

func (c *CkeditorImageupload) Get() {
	c.ServeJSON()
}

//根据上传的地方进行分类存放图片
// func (c *CkeditorImageupload) Post() {
// 	defer c.ServeJSON()
// 	var js = make(map[string]interface{})
// 	var errtext = make(map[string]interface{})
// 	c.Data["json"] = &js
// 	f, n, err := c.GetFile("upload")
// 	filename := n.Filename
// 	filenames := strings.Split(filename, ".")
// 	defer f.Close()
// 	if err != nil {
// 		beego.Debug(err)
// 		js["upload"] = 0
// 		errtext["message"] = err
// 		js["error"] = errtext
// 	} else {
// 		timestamp := time.Now().UnixNano()
// 		timestamp2 := strconv.FormatInt(timestamp, 10)
// 		timestamp11 := time.Now().Unix()
// 		tm := time.Unix(timestamp11, 0)
// 		dirname := tm.Format("2006-01-02")
// 		savepath, pathname, name, dirpath := Dir("ckeditor", dirname, timestamp2, filenames[1])
// 		if err = os.MkdirAll(dirpath, 0777); err != nil {
// 			js["uploaded"] = 0
// 			beego.Debug(err)
// 			errtext["message"] = err
// 			js["error"] = errtext
// 		} else {
// 			if err = c.SaveToFile("upload", savepath); err != nil {
// 				js["uploaded"] = 0
// 				beego.Debug(err)
// 				errtext["message"] = err
// 				js["error"] = errtext
// 			} else {
// 				if fi, err := os.OpenFile(savepath, os.O_RDWR, 0777); err != nil {
// 					beego.Debug(err)
// 				} else {
// 					defer fi.Close()
// 					if img, err := jpeg.Decode(fi); err != nil {
// 						beego.Debug(err)
// 					} else {
// 						if err := jpeg.Encode(fi, img, &jpeg.Options{Quality: 7}); err != nil {
// 							beego.Debug(err)
// 						} else {
// 							beego.Debug(`压缩成功`)
// 						}
// 					}
// 				}
// 				js["uploaded"] = 1
// 				js["fileName"] = name
// 				js["url"] = pathname

// 			}
// 		}
// 	}
// }
func (c *CkeditorImageupload) Post() {
	defer c.ServeJSON()
	var js = make(map[string]interface{})
	var errtext = make(map[string]interface{})
	c.Data["json"] = &js
	f, n, err := c.GetFile("upload")
	filename := n.Filename
	filenames := strings.Split(filename, ".")
	defer f.Close()
	if err != nil {
		beego.Debug(err)
		js["upload"] = 0
		errtext["message"] = err
		js["error"] = errtext
	} else {
		timestamp := time.Now().UnixNano()
		timestamp2 := strconv.FormatInt(timestamp, 10)
		timestamp11 := time.Now().Unix()
		tm := time.Unix(timestamp11, 0)
		dirname := tm.Format("2006-01-02")
		savepath, _, name, dirpath := Dir("ckeditor", dirname, timestamp2, filenames[1])
		if err = os.MkdirAll(dirpath, 0777); err != nil {
			js["uploaded"] = 0
			beego.Debug(err)
			errtext["message"] = err
			js["error"] = errtext
		} else {
			if err = c.SaveToFile("upload", savepath); err != nil {
				js["uploaded"] = 0
				beego.Debug(err)
				errtext["message"] = err
				js["error"] = errtext
			} else {
				osspath, _ := models.OssUpfile(savepath, savepath)
				js["uploaded"] = 1
				js["fileName"] = name
				js["url"] = osspath
			}

		}
	}
	// if err != nil {
	// 	beego.Debug(err)
	// 	c.Ctx.ResponseWriter.Write([]byte(`<script>alert("保存文件失败,请重试")</script>`))
	// 	c.StopRun()
	// }
	// c.Ctx.ResponseWriter.Write([]byte(`<script type="text/javascript">`))
	// c.Ctx.ResponseWriter.Write([]byte(`window.parent.CKEDITOR.tools.callFunction(1,'` + pathname + `','');`))
	// c.Ctx.ResponseWriter.Write([]byte(`</script>`))
	// c.StopRun()
	// inputs := c.Input()
	// b64 := inputs.Get("tp")
	// base := b64[strings.IndexByte(b64, ',')+1:]
	// res, _ := base64.StdEncoding.DecodeString(base)

	// var where string
	// if inputs.Get("where") == "" {
	// 	where = "default"
	// } else {
	// 	where = inputs.Get("where")
	// }
	//
	// if errdir := os.MkdirAll(dirpath, 0777); errdir != nil {
	// 	beego.Debug(errdir)
	// }

	// if err := ioutil.WriteFile(savepath, res, 0666); err != nil {
	// 	beego.Debug("打开文件失败:", err)
	// }
	// var img models.Mt_img
	// sess := c.GetSession("admin")
	// if sess != nil {
	// 	img.Uid, _ = strconv.Atoi(sess.(string))
	// } else {
	// 	img.Uid = 0
	// }
	// img.Name = timestamp2 + ".jpg"
	// img.Path = path
	// img.Pathname = pathname
	// img.Addtime = time.Now()
	// img.Ip = strings.Split(c.Ctx.Request.RemoteAddr, ":")[0]
	// mid, err := models.Orm().Insert(&img)
	// var json = make(map[string]interface{})
	// if err != nil {
	// 	beego.Debug("insert media数据失败:", err)
	// 	json["status"] = "0"
	// } else {
	// 	json["status"] = "1"
	// 	json["mid"] = mid
	// 	json["pathname"] = pathname
	// }
	// c.Data["json"] = json
	// c.ServeJSON()
}
